package com.c.ctgproject.fragment.review.car

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.review.ReviewCarPriceAdapter
import com.c.ctgproject.base.BaseFragment
import com.c.ctgproject.entity.review.CarReviewEntity
import com.c.ctgproject.mode.MyConstants
import com.c.ctgproject.utils.MyLog
import com.c.ctgproject.utils.getGson
import com.c.ctgproject.utils.initData
import com.c.ctgproject.utils.showToast
import com.c.ctgproject.wiget.dialog.InputDialog
import kotlinx.android.synthetic.main.fragment_wait_car.*
import kotlinx.android.synthetic.main.fragment_wait_car.mRefresh

/**
 *作者:LiBW
 *创建日期: 2020/9/9
 *描述:报废车审核 待审核
 */
class WaitCarFragment : BaseFragment(), ReviewCarPriceAdapter.CarFragmentResult, InputDialog.InPutDialogCallBack {
    val mViewModel: WaitCarViewModel by lazy { ViewModelProvider(requireActivity())[WaitCarViewModel::class.java] }
    private var page = 1
    private var size = 10
    private var mList: ArrayList<CarReviewEntity.DataBean.ListBean> = ArrayList()
    private val mAdapter: ReviewCarPriceAdapter by lazy { ReviewCarPriceAdapter(requireContext(), mList, this) }
    private val dialog: InputDialog by lazy { InputDialog(this) }
    private var type = MyConstants.AGREE
    private var mOrderId = 0
    override fun event() {
        mRecyclerView.adapter = mAdapter
        mRecyclerView.layoutManager = LinearLayoutManager(requireContext())

        //详情列表回调
        mViewModel.result.observe(this, Observer {
            MyLog.d("回调:$it")
            val data = initData(it)
            if (data.status == 0) {
                mRefresh.finishRefresh()
                mRefresh.finishLoadMore()
                val entity = getGson().fromJson(it, CarReviewEntity::class.java).data
                entity?.list?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            } else {
                requireContext().showToast(data.msg)
            }
        })
        //订单审核回调
        mViewModel.confirmOrder.observe(this, Observer {
            MyLog.d("报废车订单审核回调:$it")
            val data = initData(it)
            if (data.status == 0) {
                requireContext().showToast("提交成功")
                dialog.dismiss()
                initData()
            } else {
                requireContext().showToast(data.msg)
            }
        })

        mViewModel.error.observe(this, Observer {
            mRefresh.finishRefresh()
            mRefresh.finishLoadMore()
            requireContext().showToast(it)
        })


    }

    override fun click() {
        //下拉刷新监听
        mRefresh.setOnRefreshListener {
            initData()
        }
        //上拉加载
        mRefresh.setOnLoadMoreListener {
            page++
            mViewModel.getList(page, size)
        }
    }

    private fun initData() {
        mList.clear()
        page = 1
        mViewModel.getList(page, size)
    }

    override fun onResume() {
        super.onResume()
        initData()
    }

    override fun getLayout() = R.layout.fragment_wait_car

    //同意
    override fun agree(orderId: Int) {
        if (orderId == 0) {
            requireActivity().showToast("订单ID为空无法提交")
            return
        }
        mOrderId = orderId
        dialog.show(requireActivity().supportFragmentManager, "dialog")
        type = MyConstants.AGREE
    }

    //拒绝
    override fun refuse(orderId: Int) {
        if (orderId == 0) {
            requireActivity().showToast("订单ID为空无法提交")
            return
        }
        mOrderId = orderId
        type = MyConstants.REFUSE
        dialog.show(requireActivity().supportFragmentManager, "dialog")
    }

    override fun dialogResultText(msg: String) {
        mViewModel.orderConfirm(msg, type, mOrderId)
    }
}